'Initial Review Requirement Script  
Option Strict Off
imports System
imports PHSRAG.Insight21.Humans
imports PHSRAG.Insight21.BusinessObjects
imports FormsManagement = PHSRAG.Insight21.FormsManagement

module Script
sub Main()

    ' Always add the Sponsor
    Dim sponsorQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Sponsor Funding", FormsManagement.Constants.QuestionnaireType.Animals )
    Dim sponsor as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(sponsorQV.Id, cachePolicy, "Sponsor/Funding", false)

    Dim questionnaireAnswers as System.Collections.ArrayList = currentProtVersion.QuestionnaireAnswers
    if questionnaireAnswers.Count > 0 then
		
        Dim initialReviewQA as FormsManagement.QuestionnaireAnswer = questionnaireAnswers(0)
			
        Dim researchTypeAnswers as System.Collections.ArrayList = initialReviewQA.GetAnswers("RschType")
        Dim animalsTypeAnswers as System.Collections.ArrayList = initialReviewQA.GetAnswers("AnType")

        if researchTypeAnswers.Count > 0 and animalsTypeAnswers.Count > 0 then
            Dim researchTypeAnswer as FormsManagement.Answer = researchTypeAnswers(0)
            Dim animalsTypeAnswer as FormsManagement.Answer = animalsTypeAnswers(0)

            if (researchTypeAnswer.Value = "WHOLE" or researchTypeAnswer.Value = "PART") and animalsTypeAnswer.Value = "DEAD" then
               ' Tissue
               Dim tissueQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Tissue", FormsManagement.Constants.QuestionnaireType.Animals )
               Dim tissue as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(tissueQV.Id, cachePolicy, "Tissue Application", true)
               theProtocol.ProtocolTypeCode = "PROTOCOLTYPE_ATISSUE"
            else
               ' Standard always gets Abstract and Detailed Research Plan
               Dim abstractQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Abstract", FormsManagement.Constants.QuestionnaireType.Animals )
               Dim abstract as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(abstractQV.Id, cachePolicy, "Abstract", true)

               Dim detailedPlanQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Detailed Research Plan", FormsManagement.Constants.QuestionnaireType.Animals )
               Dim detailedPlan as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(detailedPlanQV.Id, cachePolicy, "Detailed Research Plan", true)

               ' Standard Application
               Dim standardQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Standard Application", FormsManagement.Constants.QuestionnaireType.Animals )
               Dim standard as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(standardQV.Id, cachePolicy, "Standard Application", true)
               
               ' Duration & clinical signs
               Dim durationClinicalSignsQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Duration, Clinical Signs, Endpoints and Euthanasia", FormsManagement.Constants.QuestionnaireType.Animals )
               Dim durationClinical as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(durationClinicalSignsQV.Id, cachePolicy, "Duration, Clinical Signs, Endpoints and Euthanasia", true)
               
               ' Housing
               Dim housingQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Housing", FormsManagement.Constants.QuestionnaireType.Animals )
               Dim housing as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(housingQV.Id, cachePolicy, "Housing", true)

               ' Potential Pain and Distress
               Dim ppdQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Potential Pain and Distress", FormsManagement.Constants.QuestionnaireType.Animals )
               Dim ppd as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(ppdQV.Id, cachePolicy, "Potential Pain and Distress", true)

               theProtocol.ProtocolTypeCode = "PROTOCOLTYPE_ANIMALS"
            end if
        end if

	' All Animals protocols must have a flowchart attachement
	applicationProtProcess.AddProtocolDocument ( applicationProtProcess.ProtocolId, "Flowchart", "ATCHTYPE_FLOWCHART","Flowchart", false, applicationProtProcess.Id )
    end if 

    theProtocol.Save( connectionString )
    'currentProtVersion.Save ( connectionString )
    applicationProtProcess.Save( connectionString )
end sub  
end module
